library(rgeos)
## Warning: package 'rgeos' was built under R version 3.5.1
## rgeos version: 0.3-28, (SVN revision 572)
##  GEOS runtime version: 3.6.1-CAPI-1.10.1 r0 
##  Linking to sp version: 1.3-1 
##  Polygon checking: TRUE
library(tigris)
## Warning: package 'tigris' was built under R version 3.5.1
## To enable 
## caching of data, set `options(tigris_use_cache = TRUE)` in your R script or .Rprofile.
## 
## Attaching package: 'tigris'
## The following object is masked from 'package:graphics':
## 
##     plot
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:rgeos':
## 
##     intersect, setdiff, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(leaflet)
## Warning: package 'leaflet' was built under R version 3.5.1
library(sp)
## Warning: package 'sp' was built under R version 3.5.1
library(ggmap)
## Warning: package 'ggmap' was built under R version 3.5.1
## Loading required package: ggplot2
library(maptools)
## Warning: package 'maptools' was built under R version 3.5.1
## Checking rgeos availability: TRUE
library(broom)
## Warning: package 'broom' was built under R version 3.5.1
library(httr)
## Warning: package 'httr' was built under R version 3.5.1
library(rgdal)
## Warning: package 'rgdal' was built under R version 3.5.1
## rgdal: version: 1.3-3, (SVN revision 759)
##  Geospatial Data Abstraction Library extensions to R successfully loaded
##  Loaded GDAL runtime: GDAL 2.2.3, released 2017/11/20
##  Path to GDAL shared files: C:/Users/Amanda Rodriguez/Documents/R/win-library/3.5/sf/gdal
##  GDAL binary built with GEOS: TRUE 
##  Loaded PROJ.4 runtime: Rel. 4.9.3, 15 August 2016, [PJ_VERSION: 493]
##  Path to PROJ.4 shared files: C:/Users/Amanda Rodriguez/Documents/R/win-library/3.5/sf/proj
##  Linking to sp version: 1.3-1
library(tidyverse)
## -- Attaching packages ---------------------------------------------------------------- tidyverse 1.2.1 --
## v tibble  1.4.2     v purrr   0.2.5
## v tidyr   0.8.1     v stringr 1.3.1
## v readr   1.1.1     v forcats 0.3.0
## -- Conflicts ------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
# Create a data frame that has the unique name, latitude, and longitude for each Citibike station that was present in the system in July 2014
load("~/coursework/week1/trips.RData")
citibike_stations <- trips %>% filter(ymd > "2014-06-30" & ymd < "2014-08-01") %>% select(start_station_name, start_station_latitude, start_station_longitude) %>% arrange(desc(start_station_latitude)) %>% unique() 
colnames(citibike_stations)[1] <- "station_name"
colnames(citibike_stations)[2] <- "latitude"
colnames(citibike_stations)[3] <- "longitude"
# Make a map showing the location of each Citibike station using ggmap
nyc_map <- get_map(location = c(lon = -73.98766, lat = 40.72595), maptype = "terrain", zoom = 12)
## Map from URL : http://maps.googleapis.com/maps/api/staticmap?center=40.72595,-73.98766&zoom=12&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false
ggmap(nyc_map) + geom_point(data=citibike_stations, aes(x=longitude, y = latitude))

# Do the same using leaflet, adding a popup that shows the name of the station when it's clicked on
leaflet(citibike_stations) %>%
  addTiles() %>% 
  addMarkers(~longitude, ~latitude, popup = ~station_name, data = citibike_stations) %>%
  addProviderTiles("CartoDB.Positron") %>%
  setView(-73.98, 40.75, zoom = 13)
# Then do a spatial join to combine this data frame with the Pediacities NYC neighborhood shapefile data
r <- GET('http://data.beta.nyc//dataset/0ff93d2d-90ba-457c-9f7e-39e47bf2ac5f/resource/35dd04fb-81b3-479b-a074-a27a37888ce7/download/d085e2f8d0b54d4590b1e7d1f35594c1pediacitiesnycneighborhoods.geojson')
nyc_neighborhoods <- readOGR(content(r,'text'), 'OGRGeoJSON', verbose = F)
## No encoding supplied: defaulting to UTF-8.
stations_spdf <- citibike_stations
coordinates(stations_spdf) <- ~longitude + latitude
proj4string(stations_spdf) <- proj4string(nyc_neighborhoods)
matches <- over(stations_spdf, nyc_neighborhoods)
stations <- cbind(citibike_stations, matches)
# Make a map showing the number of unique Citibike stations in each neighborhood
# First do this using ggmap where the fill color encodes the number of stations
points_by_neighborhood <- stations %>%
  group_by(neighborhood) %>%
  summarize(num_points=n())

plot_data <- tidy(nyc_neighborhoods, region = "neighborhood") %>%
  left_join(., points_by_neighborhood, by=c("id"="neighborhood")) %>%
  filter(!is.na(num_points))
## Warning: Column `id`/`neighborhood` joining character vector and factor,
## coercing into character vector
ggmap(nyc_map) + 
   geom_polygon(data=plot_data, 
                aes(x=long, y=lat, group=group, fill=num_points), alpha=0.75)

# Then do the same using leaflet, adding a popup that shows the number of stations in a neighborhood when its shape is clicked on

map_data <- geo_join(nyc_neighborhoods, points_by_neighborhood, "neighborhood", "neighborhood")

pal <- colorNumeric(palette = "RdBu",
                    domain = range(map_data@data$num_points, na.rm=T))

leaflet(map_data) %>%
  addTiles() %>% 
  addPolygons(fillColor = ~pal(num_points), popup = ~as.character(num_points)) %>% 
  addProviderTiles("CartoDB.Positron") %>%
  setView(-73.98, 40.75, zoom = 13)
# Now create a new data frame that has the total number of trips that depart from each station at each hour of the day on July 14th
trips_by_hour <- trips %>% filter(ymd == "2014-07-14") %>% mutate(hour = format(as.POSIXct(starttime) ,format = "%H")) 
colnames(trips_by_hour)[6] <- "latitude"
colnames(trips_by_hour)[7] <- "longitude"
# Do a spatial join to combine this data frame with the Pediacities NYC neighborhood shapefile data
trips_spdf <- trips_by_hour
coordinates(trips_spdf) <- ~longitude + latitude
proj4string(trips_spdf) <- proj4string(nyc_neighborhoods)
matches2 <- over(trips_spdf, nyc_neighborhoods)
trips_neighborhood <- cbind(trips_by_hour, matches2)

trips_neighborhood <- trips_neighborhood %>% group_by(start_station_name, longitude, latitude, hour, neighborhood, boroughCode,borough,X.id) %>% summarise(num_trips = n()) %>% filter(hour == "09" | hour == "13" | hour == "17" | hour == "22") 
# Make a ggmap plot showing the number of trips that leave from each neighborhood at 9am, 1pm, 5pm, and 10pm, faceted by hour, where each facet contains a map where the fill color encodes the number of departing trips in each neighborhood
ggmap(nyc_map) + 
   geom_polygon(data=trips_neighborhood, 
                aes(x=longitude, y=latitude, fill=num_trips), alpha=0.75) + facet_wrap(~hour)